#include "canwidget.h"

#include <QPushButton>
#include <QLineEdit>
#include <QComboBox>
#include <QLabel>
#include <QTimer>
#include <QGridLayout>
#include <QString>
#include <QList>
#include <QTimer>
#include <QTextEdit>
#include <unistd.h>
#include <QListView>

#include "weiqianfunctions.h"
#include "canreadthread.h"
#include "configargs.h"

CanWidget::CanWidget(QWidget *parent) : QWidget(parent)
{
   im = new InputMethod;
	
	cansentcont 	=	0;
	canreceivecont	=	0;
	opengflag		=	0;
	canautoflag 	=	0;
	
	QLabel			*pBaudLabel 			= new QLabel(this);
	QLabel			*pCanIdLabel			= new QLabel(this);
	QLabel			*pSentCountLabel		= new QLabel(this);
	QLabel			*pRecevCountLabel		= new QLabel(this);
	
	pOpenBut	   = new QPushButton(this);
	pCloseBut	   = new QPushButton(this);
	pSentBut	   = new QPushButton(this);
	pClearBut	   = new QPushButton(this);
	
	m_pAutoRadioBut   = new QCheckBox;
	m_pHexorChar = new QCheckBox;
		
	m_pAutoRadioBut->setStyleSheet("QCheckBox::indicator {width: 15px; height: 15px;}QCheckBox::indicator:checked {image: url(:/image/linuxdesktop/settingsicon/checkbox.png);}");
	m_pHexorChar->setStyleSheet("QCheckBox::indicator {width: 15px; height: 15px;}QCheckBox::indicator:checked {image: url(:/image/linuxdesktop/settingsicon/checkbox.png);}");

	m_pAutoTimer			=new QTimer;
	m_pTimeEdit 			= new QLineEdit;
	m_pSentCountEdit		= new QLineEdit;
	m_pReciveCountEdit		= new QLineEdit;
	m_pReceiveTimer 		= new QTimer;

	m_pReceiveEdit	= new QPlainTextEdit;
	m_pBaudCombo	= new QComboBox;
	m_pCanIdEdit	= new QLineEdit;
	m_pSendEdit 	= new QLineEdit;

	m_pCanIdEdit->installEventFilter(im);
	m_pCanIdEdit->setObjectName("m_pCanIdEdit");

	m_pSendEdit->installEventFilter(im);
	m_pSendEdit->setObjectName("m_pSendEdit");

	m_pTimeEdit->installEventFilter(im);
	m_pTimeEdit->setObjectName("m_pTimeEdit");

				 //   connect(im,SIGNAL(setImObjName(QString)),this,SLOT(createImConnect(QString)));//connect the input method
	connect(im,SIGNAL(setImObjName(QString)),this,SLOT(createImConnect(QString)));//connect the input method
	QHBoxLayout * subHLayout1 = new QHBoxLayout;
	QHBoxLayout * subHLayout2 = new QHBoxLayout;
	QVBoxLayout * subVLayout = new QVBoxLayout;
	QGridLayout   *pMainLayout		  = new QGridLayout;

	if(g_lcdsize == LCDSIZE_480x272)
	{
		pBaudLabel		->setText(tr("CAN波特率："));
		pCanIdLabel 	->setText(tr("帧ID："));
		pOpenBut		->setText(tr("打开"));
		pCloseBut		->setText(tr("关闭"));
		pSentBut		->setText(tr("发送"));
		pClearBut		->setText(tr("清空"));
		m_pAutoRadioBut 	   ->setText(tr("自发(ms):"));
	
	
		subHLayout1->addWidget(m_pAutoRadioBut);

		subHLayout1->addWidget(m_pTimeEdit);
		m_pTimeEdit->setMaximumSize(120,50);
		//	subHLayout1->addWidget(pMsLabel);

		subHLayout1->addWidget(m_pHexorChar);
		subHLayout2->addWidget(pSentCountLabel);
		subHLayout2->addWidget(m_pSentCountEdit);
		m_pSentCountEdit->setMaximumSize(120,50);
		//	m_pSentCountEdit->setEnabled(false);
		subHLayout2->addWidget(pRecevCountLabel);
		subHLayout2->addWidget(m_pReciveCountEdit);
		m_pReciveCountEdit->setMaximumSize(120,50);
		//	m_pReciveCountEdit->setEnabled(false);


		subVLayout->addLayout(subHLayout1);
		subVLayout->addLayout(subHLayout2);
		
		pMainLayout->addWidget(pBaudLabel,			0, 0, 1, 2);
		pMainLayout->addWidget(m_pBaudCombo,		  1, 0, 1, 2);
		pMainLayout->addWidget(pCanIdLabel, 		2, 0, 1, 2);
		pMainLayout->addWidget(m_pCanIdEdit,		  3, 0, 1, 2);
		pMainLayout->addWidget(pOpenBut,			  4, 0, 1, 1);
		pMainLayout->addWidget(pCloseBut,		  4, 1, 1, 1);
		pMainLayout->addWidget(m_pReceiveEdit,	  0, 2, 4, 8);
		pMainLayout->addWidget(m_pSendEdit, 	  4, 2, 1, 8);
		pMainLayout->addLayout(subVLayout,		  5, 0, 1, 6);

		pMainLayout->addWidget(pClearBut,		  5, 7, 1, 1);
		pMainLayout->addWidget(pSentBut,			  5, 8, 1, 1);


	}else{
		pBaudLabel		->setText(tr("CAN波特率："));
		pCanIdLabel 	->setText(tr("帧ID："));
		pOpenBut		->setText(tr("打开"));
		pCloseBut		->setText(tr("关闭"));
		pSentBut		->setText(tr("发送"));
		pClearBut		->setText(tr("清空"));
		m_pAutoRadioBut 	   ->setText(tr("自动发送周期 (ms) : "));

			
	
		subHLayout1->addWidget(m_pAutoRadioBut);

		subHLayout1->addWidget(m_pTimeEdit);
		m_pTimeEdit->setMaximumSize(120,50);
		 //   subHLayout1->addWidget(pMsLabel);

		subHLayout2->addWidget(m_pHexorChar);
		subHLayout2->addWidget(pSentCountLabel);
		subHLayout2->addWidget(m_pSentCountEdit);
		m_pSentCountEdit->setMaximumSize(120,50);
		//	m_pSentCountEdit->setEnabled(false);
		subHLayout2->addWidget(pRecevCountLabel);
		subHLayout2->addWidget(m_pReciveCountEdit);
		m_pReciveCountEdit->setMaximumSize(120,50);
		//	m_pReciveCountEdit->setEnabled(false);


		subVLayout->addLayout(subHLayout1);
		subVLayout->addLayout(subHLayout2);
		
		pMainLayout->addWidget(pBaudLabel,			0, 0, 1, 2);
		pMainLayout->addWidget(m_pBaudCombo,		1, 0, 1, 2);
		pMainLayout->addWidget(pCanIdLabel, 		2, 0, 1, 2);
		pMainLayout->addWidget(m_pCanIdEdit,		3, 0, 1, 2);
		pMainLayout->addWidget(pOpenBut,			4, 0, 1, 1);
		pMainLayout->addWidget(pCloseBut,			4, 1, 1, 1);
		pMainLayout->addWidget(m_pReceiveEdit,		0, 2, 4, 8);
		pMainLayout->addWidget(m_pSendEdit, 		4, 2, 1, 8);
		pMainLayout->addLayout(subVLayout,			5, 0, 1, 6);

		pMainLayout->addWidget(pClearBut,			5, 7, 1, 1);
		pMainLayout->addWidget(pSentBut,			5, 8, 1, 1);

	}

  

	QList<QString> baudList;
	baudList	<<"50000"
				<<"100000"
				<<"125000"
				<<"250000";

	m_pBaudCombo->addItems(baudList);
	m_pBaudCombo->setView(new QListView());
	m_pBaudCombo->setCurrentIndex(0);

	pBaudLabel			->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
	pCanIdLabel 		->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
	pOpenBut			->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
	pCloseBut			->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
	pSentBut			->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
	pClearBut			->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
	m_pReceiveEdit		->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
	m_pBaudCombo		->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
	m_pCanIdEdit		->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
	m_pSendEdit 		->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
	m_pAutoRadioBut 	->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
	pSentCountLabel 	->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    m_pReceiveEdit->document()->setMaximumBlockCount(1000);
//	  m_pAutoRadioBut	  ->setText(tr("自动发送周期 (ms) : "));
	m_pHexorChar		->setText(tr("HEX"));
	pSentCountLabel 	->setText(tr("发"));
	pRecevCountLabel	->setText(tr("收"));


	m_pReceiveEdit->setReadOnly(true);
	m_pReceiveEdit->setMaximumBlockCount(500);	  //最大行数

	this->setLayout(pMainLayout);

	connect(pOpenBut,		SIGNAL(clicked()),	this,	SLOT(openCan()));
	connect(pCloseBut,		SIGNAL(clicked()),	this,	SLOT(closeCan()));
	connect(pClearBut,		SIGNAL(clicked()),	this,	SLOT(clear()));
	connect(pSentBut,		SIGNAL(clicked()),	this,	SLOT(send()));

	m_pCanIdEdit->setText("1");
	m_pSendEdit->setText("12345678");
	m_pTimeEdit->setText(tr("10"));
	m_pSentCountEdit->setText(tr("0"));
	m_pReciveCountEdit->setText(tr("0"));


	m_pThread = new CanReadThread();
	connect(m_pThread, SIGNAL(receiveSignal(int,QString,int)), this, SLOT(receiveData(int,QString,int)));
	connect(m_pAutoRadioBut,	SIGNAL(clicked()),	   this,   SLOT(Autohave()));
	connect(m_pHexorChar,	 SIGNAL(clicked()), 	this,	SLOT(HexOrChar()));
	connect(m_pAutoTimer,	   SIGNAL(timeout()),	  this,   SLOT(timeAuto()));

	pCloseBut->setEnabled(false);
	pSentBut->setEnabled(false);

	
	pCloseBut->setStyleSheet("QPushButton{background-color: #b4d3e9; color: #ffffff; border: 1px solid #b4d3e9;}");
	pSentBut->setStyleSheet("QPushButton{background-color: #b4d3e9; color: #ffffff; border: 1px solid #b4d3e9;}");

}
CanWidget::~CanWidget(){
   // delete m_pThread;
    closeCan();
}

void CanWidget::Autohave()
{

    if(m_pAutoRadioBut->isChecked()) {
        if( (!IsNub(m_pTimeEdit->text())) ) // bu shi shu zi
             m_pTimeEdit->setText(tr("300"));
        tmiems=m_pTimeEdit->text().toInt();
        m_pAutoTimer->start(tmiems);
        pSentBut->setEnabled(false);
    }else{
        m_pAutoTimer->stop();
        pSentBut->setEnabled(true);
    }
}

void CanWidget::timeAuto()
{
   // if(haveopen)
    if( (opengflag) && (1==canautoflag) )
  {
        send();
        m_pSentCountEdit->setText(QString::number(cansentcont));
    }

}

void CanWidget::createImConnect(QString ObjName)
{
    disconnect(im->keyboard,SIGNAL(setvalue(QString)),m_pSendEdit,SLOT(setText(QString)));
    disconnect(im->keyboard,SIGNAL(setvalue(QString)),m_pCanIdEdit,SLOT(setText(QString)));
    disconnect(im->keyboard,SIGNAL(setvalue(QString)),m_pTimeEdit,SLOT(setText(QString)));
    if(ObjName=="m_pSendEdit")
        connect(im->keyboard,SIGNAL(setvalue(QString)),m_pSendEdit,SLOT(setText(QString)));
    if(ObjName=="m_pCanIdEdit")
        connect(im->keyboard,SIGNAL(setvalue(QString)),m_pCanIdEdit,SLOT(setText(QString)));
    if(ObjName=="m_pTimeEdit")
        connect(im->keyboard,SIGNAL(setvalue(QString)),m_pTimeEdit,SLOT(setText(QString)));

}

void CanWidget::openCan()
{
	m_pBaudCombo->setEnabled(false);
	m_pAutoRadioBut->setEnabled(true);
	m_pTimeEdit->setEnabled(false);
	pOpenBut->setEnabled(false);
	m_pCanIdEdit->setEnabled(false);

	pCloseBut->setEnabled(true);
	pSentBut->setEnabled(true);

	pOpenBut->setStyleSheet("QPushButton{background-color: #b4d3e9; color: #ffffff; border: 1px solid #b4d3e9;}");
	pCloseBut->setStyleSheet("QPushButton{background-color: #4a95c9; color: #ffffff; border: 1px solid #4a95c9;}");
	pSentBut->setStyleSheet("QPushButton{background-color: #4a95c9; color: #ffffff; border: 1px solid #4a95c9;}");
	m_pBaudCombo->setStyleSheet("QComboBox {border: 2px solid #b4d3e9; background-color: #b4d3e9;   border-radius: 0px;	 color: #ffffff;} QComboBox::drop-down {background-color: #b4d3e9;}");

	
    system("ifconfig -a | grep can >/tmp/HAVEcan.dat");
    QFile filetmpwlan("/tmp/HAVEcan.dat");
    filetmpwlan.open(QFile::ReadOnly);
    QString stmpwlan =filetmpwlan.readAll();
    QString stmp2wlan="can";

    if(!stmpwlan.contains(stmp2wlan) ){
        //"No have can");
        opengflag=0;

        QDialog * setdialog = new QDialog;
        QLabel * settmp = new QLabel;
        settmp->setText(tr("标配版没有CAN模块"));
        settmp->setStyleSheet("color:red;");
        QTimer*timer = new QTimer(setdialog);
        timer->start(600);
        connect(timer,SIGNAL(timeout()),setdialog,SLOT(close()));
        QGridLayout *tmplayout = new QGridLayout;
        tmplayout->addWidget(settmp,0,0,1,1);
        setdialog->setLayout(tmplayout);
        setdialog->setWindowFlags(Qt::FramelessWindowHint);
        setdialog->exec();
    }else {
        canautoflag=1;
        opengflag=1;
    }
    if( (!IsNub(m_pCanIdEdit->text())) ) // bu shi shu zi
         m_pCanIdEdit->setText(tr("1"));

    WeiqianFunctions::InitCan(m_pBaudCombo->currentText().toInt());
    if(WeiqianFunctions::OpenCan() > 0) {
        m_pThread->start();
    }
}

void CanWidget::closeCan()
{
    canautoflag=0;

    m_pAutoTimer->stop();
    m_pAutoRadioBut->setCheckState(Qt::Unchecked);

    m_pBaudCombo->setEnabled(true);
	m_pAutoRadioBut->setEnabled(false);
	m_pTimeEdit->setEnabled(true);
	m_pCanIdEdit->setEnabled(true);
	
	pOpenBut->setEnabled(true);
	pCloseBut->setEnabled(false);
	pSentBut->setEnabled(false);

	pOpenBut->setStyleSheet("QPushButton{background-color: #4a95c9; color: #ffffff; border: 1px solid #4a95c9;}");
	pCloseBut->setStyleSheet("QPushButton{background-color: #b4d3e9; color: #ffffff; border: 1px solid #b4d3e9;}");
	pSentBut->setStyleSheet("QPushButton{background-color: #b4d3e9; color: #ffffff; border: 1px solid #b4d3e9;}");
	m_pBaudCombo	->setStyleSheet("QComboBox {border: 2px solid #4a95c9; background-color: #4a95c9;	border-radius: 0px;    color: #ffffff;}");
	
    opengflag=0;
    if(m_pThread->isRunning()) {
        m_pThread->stop();
        usleep(100000);
    }
    WeiqianFunctions::CloseCan();

}

void CanWidget::clear()
{
    closeCan();
    m_pReceiveEdit->clear();
    m_pSendEdit->clear();
    canreceivecont=0;
    m_pReciveCountEdit->setText("0");
    cansentcont=0;
    m_pSentCountEdit->setText("0");
}

void CanWidget::send()
{
  /*  char *pBuffcrl = new char[8];
    pBuffcrl[0]=0x01;
    pBuffcrl[1]=0x02;
    pBuffcrl[2]=0x03;
    pBuffcrl[3]=0x00;
    pBuffcrl[4]=0x05;
    pBuffcrl[5]=0x00;
    pBuffcrl[6]=0x07;
    pBuffcrl[7]=0x08;*/
    if(opengflag)
    if(m_pSendEdit->text().count() > 0) {
        if( (!IsNub(m_pCanIdEdit->text())) ) // bu shi shu zi
             m_pCanIdEdit->setText(tr("1"));
        //QByteArray temp = m_pSendEdit->text().toUtf8();
        //const char *pBuff = temp.data();
       // qDebug("datalen=%d",m_pSendEdit->text().length());
       
        WeiqianFunctions::CanWrite(m_pCanIdEdit->text().toInt(), m_pSendEdit->text().toLocal8Bit().data(),(m_pSendEdit->text().length()));


//		qDebug() << pBuff;

        if(cansentcont<18446744073709551610)
        	cansentcont += m_pSendEdit->text().length();
        else
            cansentcont=0;
        m_pSentCountEdit->setText(QString::number(cansentcont));
    }
  //  delete pBuffcrl;
}

void CanWidget::receiveData(const int id, const QString data, const int lenght)
{
      m_pReceiveEdit->appendPlainText("Id:" + QString::number(id) + "  Data:" + data);
      if(m_pReceiveEdit->document()->blockCount() >= 1000){
          m_pReceiveEdit->document()->clear();
      }
//	  qDebug() << data;
    //  m_pReciveCountEdit->setText("");
    //  QString tmp =QString.toInt(receivecont);
      m_pReciveCountEdit->setText(QString::number(canreceivecont));
}

void CanWidget::HexOrChar()
{
	if(m_pThread->HexOrChar_flag == 0)
	{
		m_pThread->HexOrChar_flag = 1;
//		qDebug() << "is char now";
	}
	else
	{
//		qDebug() << "is hex now";
		m_pThread->HexOrChar_flag = 0;
	}
}


